package tcpConn

import (
	"cli-desktop/app"
    "cli-desktop/proto"
    "cli-desktop/public"
	"time"
)

/*
*  -*- coding:utf8 -*-
*
*  命令相关处理
*
*  @Author  : chengms
*  @WebSite : chengms.com
*  @Email   : chengms2012@163.com
*  @TIME    : 2020/8/24 23:16
*  @File    : handler.go
 */

func CmdHandler(cmdBuf proto.CommandBuf)  {
	switch cmdBuf.CmdType {
	case proto.CmdLogin:
		app.UserOp.ReceiveLoginMsg(cmdBuf.Data)
	case proto.CmdRegister:
		app.UserOp.ReceiveRegisterMsg(cmdBuf.Data)
	case proto.CmdBroadCase:
		public.Loggerf.Info(cmdBuf)
		
	default:
		public.Loggerf.Warnf("UnSupport message: %v", cmdBuf)
	}

}

func SendHearbeat() {
	sendCmd := proto.NewSendCommand(proto.CmdHeartbeat)
	sleepTimer := 3 * time.Second

	for {
		time.Sleep(sleepTimer)
		err := sendCmd.SendHeartbeat()
		if err != nil {
			public.Loggerf.Errorf("Send hearbeat err: %v", err)
			public.Loggerf.Infof("Server disconnect...")
			_ = proto.ConnSvc.SvcConn.Close()
			app.UserOp.Info.UserStatus = app.StsOffLine
			return
		}
	}
}

